package org.example.filter;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if ("sxg".equals(username) && "123".equals(password)) {
            request.getSession().setAttribute("user", username);
            response.sendRedirect("welcome.html");
        } else {
//            request.setAttribute("errorMessage", "Invalid username or password");
//            PrintWriter out = response.getWriter();
//            out.println("<html><body>");
//            out.println("<script>alert('Invalid username or password'); window.history.back();</script>");
//            out.println("</body></html>");
//            request.getRequestDispatcher("login.html").forward(request, response);
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<html><body>");
            out.println("<script>alert('Invalid username or password'); window.location.href='login.html';</script>");
            out.println("</body></html>");
        }
    }
}